﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using exsportance.Silverlight.Common.Utils;
using System.Text.RegularExpressions;

namespace exsportance.Silverlight.Validate.Validators
{
    /// <summary>
    /// Validator that returns true if and only if value contains only digit characters
    /// </summary>
    public class DigitsValidator : BaseValidator
    {
        #region members
        /// <summary>
        /// Validation failure message key for when the value contains non-digit characters
        /// </summary>
        private const string NOT_DIGITS = "notDigits";

        /// <summary>
        /// Validation failure message key for when the value is an empty string
        /// </summary>
        private const string STRING_EMPTY = "stringEmpty";

        protected Dictionary<string, string> _messagesTemplates = new Dictionary<string, string>(){
        {DigitsValidator.NOT_DIGITS,"'%value%' contains not only digit characters"},
        {DigitsValidator.STRING_EMPTY,"'%value%' is an empty string"},
        };

        #endregion

        #region properties

        #endregion

        #region constructor
        public DigitsValidator()
            : base()
        {

        }
        #endregion

        #region protected methods
        /// <summary>
        /// Returns true if and only if value contains only digit characters
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected override bool CheckValid(object value)
        {
            string valueString = value as string;

            rememberValue(valueString);

            if (valueString.IsNullOrEmpty())
            {
                raiseMessage(DigitsValidator.STRING_EMPTY);
                return false;
            }

            // unicode
            string pattern = @"[^\p{N}]";

            if (Regex.IsMatch(valueString, pattern, RegexOptions.IgnoreCase))
            {
                raiseMessage(DigitsValidator.NOT_DIGITS);
                return false;
            }

            return true;
        }

        protected override System.Collections.Generic.Dictionary<string, string> getMessageTemplates()
        {
            return _messagesTemplates;
        }
        #endregion
    }
}
